
// Open MoneySpinner Suite v1
// An open source business management software system written in Java and MySQL
// Recommended IDE is NetBeans IDE 7.0.1
// Support Web Site: http://www.milliscript.com
//
// Copyright (C) 2014, Abiodun Aremu, Ibadan/NIGERIA.
// Open MoneySpinner Suite is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package openmoneyspinnersuite;

/**
 *
 * @author Abiodun Aremu
 */

import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import java.awt.image.*;
import java.util.*;

final class Profile extends javax.swing.JInternalFrame {
    
    Systems systems=new Systems();
    Object[][] tableObject=new Object[][]{{"1",null,null,null,null,null,null,null}};
    String addInUse="N";
    String delInUse="N";
    PreparedStatement statement;
    ResultSet result;
    ResultSet pictureResult;
    String[] dbDesignation;
    String[] dbBank;
    String[] dbDepartment;
    Thread profileThread=new Thread();
    Thread profileTitleThread=new Thread();
    
    /** Creates new form NewCreditOrder */
     Profile() {
        initComponents();
        reset();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jEmployeePanel = new javax.swing.JPanel();
        jRefreshButton = new javax.swing.JButton();
        jProfileNameTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        registerButton = new javax.swing.JButton();
        jCodePrefixTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        delButton = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jComboBox1 = new javax.swing.JComboBox();
        jCheckBox2 = new javax.swing.JCheckBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(openmoneyspinnersuite.OpenMS_Finance_App.class).getContext().getResourceMap(Profile.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setFrameIcon(resourceMap.getIcon("Form.frameIcon")); // NOI18N
        setName("Form"); // NOI18N
        setVisible(true);

        jEmployeePanel.setName("jEmployeePanel"); // NOI18N

        jRefreshButton.setIcon(resourceMap.getIcon("jRefreshButton.icon")); // NOI18N
        jRefreshButton.setText(resourceMap.getString("jRefreshButton.text")); // NOI18N
        jRefreshButton.setName("jRefreshButton"); // NOI18N
        jRefreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRefreshButtonActionPerformed(evt);
            }
        });

        jProfileNameTextField.setText(resourceMap.getString("jProfileNameTextField.text")); // NOI18N
        jProfileNameTextField.setName("jProfileNameTextField"); // NOI18N
        jProfileNameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jProfileNameTextFieldActionPerformed(evt);
            }
        });
        jProfileNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jProfileNameTextFieldKeyTyped(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        registerButton.setFont(resourceMap.getFont("registerButton.font")); // NOI18N
        registerButton.setIcon(resourceMap.getIcon("registerButton.icon")); // NOI18N
        registerButton.setText(resourceMap.getString("registerButton.text")); // NOI18N
        registerButton.setName("registerButton"); // NOI18N
        registerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerButtonActionPerformed(evt);
            }
        });

        jCodePrefixTextField.setName("jCodePrefixTextField"); // NOI18N
        jCodePrefixTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCodePrefixTextFieldActionPerformed(evt);
            }
        });
        jCodePrefixTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jCodePrefixTextFieldKeyTyped(evt);
            }
        });

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cancelButton.setFont(resourceMap.getFont("cancelButton.font")); // NOI18N
        cancelButton.setIcon(resourceMap.getIcon("cancelButton.icon")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "S/N", "Attribute", "Type", "Default", "String Length", "Referenced Table", "Optional", "Editable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jTable1.setName("jTable1"); // NOI18N
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jTable1.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTable1.columnModel.title6")); // NOI18N
        jTable1.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTable1.columnModel.title0")); // NOI18N
        jTable1.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTable1.columnModel.title1")); // NOI18N
        jTable1.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTable1.columnModel.title2")); // NOI18N
        jTable1.getColumnModel().getColumn(4).setHeaderValue(resourceMap.getString("jTable1.columnModel.title4")); // NOI18N
        jTable1.getColumnModel().getColumn(5).setHeaderValue(resourceMap.getString("jTable1.columnModel.title5")); // NOI18N
        jTable1.getColumnModel().getColumn(6).setHeaderValue(resourceMap.getString("jTable1.columnModel.title3")); // NOI18N
        jTable1.getColumnModel().getColumn(7).setHeaderValue(resourceMap.getString("jTable1.columnModel.title7")); // NOI18N

        addButton.setIcon(resourceMap.getIcon("addButton.icon")); // NOI18N
        addButton.setText(resourceMap.getString("addButton.text")); // NOI18N
        addButton.setName("addButton"); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        delButton.setIcon(resourceMap.getIcon("delButton.icon")); // NOI18N
        delButton.setText(resourceMap.getString("delButton.text")); // NOI18N
        delButton.setName("delButton"); // NOI18N
        delButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delButtonActionPerformed(evt);
            }
        });

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-- Primary Table --" }));
        jComboBox1.setEnabled(false);
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jCheckBox2.setText(resourceMap.getString("jCheckBox2.text")); // NOI18N
        jCheckBox2.setName("jCheckBox2"); // NOI18N
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jEmployeePanelLayout = new javax.swing.GroupLayout(jEmployeePanel);
        jEmployeePanel.setLayout(jEmployeePanelLayout);
        jEmployeePanelLayout.setHorizontalGroup(
            jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(delButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(registerButton)
                .addGap(18, 18, 18)
                .addComponent(cancelButton)
                .addContainerGap(293, Short.MAX_VALUE))
            .addGroup(jEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(618, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
            .addGroup(jEmployeePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jEmployeePanelLayout.createSequentialGroup()
                        .addComponent(jCheckBox2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 684, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jEmployeePanelLayout.createSequentialGroup()
                        .addGroup(jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckBox1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jEmployeePanelLayout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jEmployeePanelLayout.createSequentialGroup()
                                .addComponent(jProfileNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                                .addGap(124, 124, 124)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCodePrefixTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(78, 78, 78))
        );
        jEmployeePanelLayout.setVerticalGroup(
            jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEmployeePanelLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jProfileNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jCodePrefixTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(2, 2, 2)
                .addComponent(jCheckBox2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jEmployeePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(delButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRefreshButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEmployeePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEmployeePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        
    if(profileThread.isAlive())
    {
        try{
            profileThread.stop();
            profileTitleThread.stop();
            System.out.println("Threads successfully stopped.");
        }catch(Exception e){System.out.println("Thread stopping error: "+e);}
        this.setTitle("::. New Profile");
        enableControls();
    }
    else
    this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

private void jCodePrefixTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jCodePrefixTextFieldKeyTyped

    String value=jCodePrefixTextField.getText();
    if(value.length()>2)
    {
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Maximum Number of Character (2) Exceeded ","ERROR !",JOptionPane.ERROR_MESSAGE);
        String newValue=String.copyValueOf(value.toCharArray(),0,1);
        jCodePrefixTextField.setText(newValue);
    }
}//GEN-LAST:event_jCodePrefixTextFieldKeyTyped

private void jCodePrefixTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCodePrefixTextFieldActionPerformed

        String value=jCodePrefixTextField.getText();
        if(value.length()>2)
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Maximum Number of Character (2) Exceeded ","ERROR !",JOptionPane.ERROR_MESSAGE);
            String newValue=String.copyValueOf(value.toCharArray(),0,1);
            jCodePrefixTextField.setText(newValue);
        }
}//GEN-LAST:event_jCodePrefixTextFieldActionPerformed

private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerButtonActionPerformed
        int x=tableObject.length;
        int y=x+1;
        for(int z=0;z<x;z++)
        {
            if(jTable1.getValueAt(z,1)==null||jTable1.getValueAt(z,2)==null)
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"NULL VALUE FOUND IN TABLE ! ","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        int b=0;
        while(b<x)
        {
            String value1=jTable1.getValueAt(b,1).toString();
            String value2=jTable1.getValueAt(b,2).toString();
            String value3="";
            String value4="";
            String value5="";
            if(jTable1.getValueAt(b,3)!=null)value3=jTable1.getValueAt(b,3).toString();
            if(jTable1.getValueAt(b,4)!=null)value4=jTable1.getValueAt(b,4).toString();
            if(jTable1.getValueAt(b,5)!=null)value5=jTable1.getValueAt(b,5).toString();
           
            if(!systems.isStringSpacedAlphaNumeric(value1.trim()))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(b+1)+"\nATTRIBUTE CAN ONLY BE A SPACED ALPHANUMERIC","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(value2.equalsIgnoreCase("String")&&(!systems.isStringDigit(value4)||value4.isEmpty()))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(b+1)+"\nPLEASE ENTER A LENGTH FOR TYPE STRING","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(value2.equalsIgnoreCase("Reference")&&value5.isEmpty())
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(b+1)+"\nPLEASE SELECT A TABLE ID FOR TYPE REFERENCE","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(value2.equalsIgnoreCase("Date/Time")&&(!systems.isDateFormat(value3)&&!value3.isEmpty()))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(b+1)+"\nPLEASE ENTER DEFAULT DATE IN YYYY/MM/DD FORMAT","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(value2.equalsIgnoreCase("Integer")&&(!systems.isStringDigit(value3)&&!value3.isEmpty()))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(b+1)+"\nPLEASE ENTER AN INTEGER VALUE AS DEFAULT INTEGER","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            if(value2.equalsIgnoreCase("Decimal")&&(!systems.isStringDecimal(value3)&&!value3.isEmpty()))
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"ERROR AT ROW:"+(b+1)+"\nPLEASE ENTER A DECIMAL VALUE AS DEFAULT DECIMAL","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
        b+=1;
        if(b==x){break;}
        }

        if(jProfileNameTextField.getText().trim().isEmpty())
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Please Enter Profile Name","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }if(!systems.isStringSpacedAlphaNumeric(jProfileNameTextField.getText().trim()))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Profile name can only be a spaced alphanumeric ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        String profileName=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where vName='"+jProfileNameTextField.getText().trim()+"'", 1);
        if((profileName==null)==false)
        {
            if(profileName.isEmpty()==false)
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Profile name already exists ","ERROR !",JOptionPane.ERROR_MESSAGE);
                return; 
            }
        }
        if(jCodePrefixTextField.getText().trim().isEmpty()&&jCheckBox1.isSelected()==false)
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Please Enter Code Prefix","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }     
        if(!systems.isStringLetter(jCodePrefixTextField.getText().trim()))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Profile code prefix can only be letters","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }     
        if(jCheckBox1.isSelected()&&jComboBox1.getSelectedItem().toString().equalsIgnoreCase("-- Primary Reference --"))
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Please select a primary reference for this profile","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;            
        }
        String prefixExists=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where cPrefix='"+jCodePrefixTextField.getText().trim()+"'", 3);
        
        if(!prefixExists.isEmpty())
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Code prefix already exist","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }        
        else{
            /* Execute submit thread */
            profileThread=new Thread(new Runnable() {
                public void run() {
                    disableControls();
                    submitData();
                    reset();
                }
            });
            profileTitleThread=new Thread(new Runnable() {
                public void run() {
                    while(profileThread.isAlive())setProcessingTitle(profileTitleThread);
                    enableControls();
                    System.out.println("profileThread stopped runing.");
                }
            });
            profileThread.start();
            profileTitleThread.start();
        }
}//GEN-LAST:event_registerButtonActionPerformed

private void jProfileNameTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jProfileNameTextFieldKeyTyped

        String value=jProfileNameTextField.getText();
        if(value.length()>49)
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Maximum Number of Character (20) Exceeded ","ERROR !",JOptionPane.ERROR_MESSAGE);
            String newValue=String.copyValueOf(value.toCharArray(),0, 19);
            jProfileNameTextField.setText(newValue);
        }
}//GEN-LAST:event_jProfileNameTextFieldKeyTyped

private void jProfileNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jProfileNameTextFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jProfileNameTextFieldActionPerformed

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
    addRow();
}//GEN-LAST:event_addButtonActionPerformed

private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
    deleteRow();
}//GEN-LAST:event_delButtonActionPerformed

private void jRefreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRefreshButtonActionPerformed

        profileThread=new Thread(new Runnable() {
            public void run() {
            disableControls();
            refreshTable();
            }
        });
        profileTitleThread=new Thread(new Runnable() {
            public void run() {
                while(profileThread.isAlive())setProcessingTitle(profileTitleThread);
                enableControls();
                System.out.println("newProfileThread stopped runing.");
            }
        });
        profileThread.start();
        profileTitleThread.start();
}//GEN-LAST:event_jRefreshButtonActionPerformed

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        String profileCode=jComboBox1.getSelectedItem().toString();
        if(profileCode.equalsIgnoreCase("-- Select --"))
        {
            resetProfileCode();
        }
}//GEN-LAST:event_jComboBox1ActionPerformed

    void resetProfileCode()
    {
        String[] orderCode=systems.getTableDataArray(OpenMSApp.Database_A," ReferenceTable",2);
        orderCode[0]="-- Primary Reference --";
        jComboBox1.setModel(new DefaultComboBoxModel(orderCode));
    }
private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
    if(jCheckBox1.isSelected())
    {
        jComboBox1.setEnabled(true);
        jCodePrefixTextField.setEnabled(false);
        jCodePrefixTextField.setText(" ");
    }
    else
    {
        jComboBox1.setEnabled(false);
        jCodePrefixTextField.setEnabled(true);
    }
}//GEN-LAST:event_jCheckBox1ActionPerformed

private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBox2ActionPerformed
                               
    private void setProcessingTitle(Thread thread)
    {
        try{
            this.setTitle("::. New Profile - Processing Request");
            thread.sleep(500);
            this.setTitle("::. New Profile - Processing Request.");
            thread.sleep(500);
            this.setTitle("::. New Profile - Processing Request..");
            thread.sleep(500);
            this.setTitle("::. New Profile - Processing Request...");
            thread.sleep(500);
        }catch(Exception e){System.out.println("Profile Title thread"+e);}
    }   
   void disableControls()
   {
   
   }
   void enableControls()
   {
    this.setTitle("::. New Profile");
   }
   void reset()
   {
       resetFields();
       resetType();
       resetRefTable();
       refreshTable();
       resetProfileCode();
   }
   void resetCombo()
   {
       resetType();
       resetProfileCode();
       resetRefTable();
   }
   void resetFields()
   {
       jProfileNameTextField.setText("");
       jCodePrefixTextField.setText("");
       jCheckBox1.setSelected(false);
   }
    void resetType()
    {
        String[] dbItem={"Date/Time","Decimal","Integer","Reference","String","Text"};
        javax.swing.JComboBox combo=new javax.swing.JComboBox(dbItem);
        javax.swing.table.TableColumn comboColumn=jTable1.getColumnModel().getColumn(2);
        comboColumn.setCellEditor(new javax.swing.DefaultCellEditor(combo));
    }
    void resetRefTable()
    {
        String[] dbItem=systems.getTableDataArray(OpenMSApp.Database_A, "ReferenceTable", 2);
        javax.swing.JComboBox combo=new javax.swing.JComboBox(dbItem);
        javax.swing.table.TableColumn comboColumn=jTable1.getColumnModel().getColumn(5);
        comboColumn.setCellEditor(new javax.swing.DefaultCellEditor(combo));
    }
    void deleteRow()
    {
        delInUse="Y";
        System.out.println("delete In Use"+delInUse);
        int x=tableObject.length;
        int y=x-1;
        Object[][] holdObject=new Object[y][8];
        if(x==1)
        {
            JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"Can Not Delete Row \n   Invalid Operation ! ","ERROR !",JOptionPane.ERROR_MESSAGE);
            return;
        }
        for(int z=0;z<x-1;z++)
        {
            if(jTable1.getValueAt(z,1)!=null&&jTable1.getValueAt(z,2)!=null)
            {
                holdObject[z][0]=z+1;
                holdObject[z][1]=jTable1.getValueAt(z,1).toString();
                holdObject[z][2]=jTable1.getValueAt(z,2).toString();
                if(jTable1.getValueAt(z,3)==null)holdObject[z][3]="";else holdObject[z][3]=jTable1.getValueAt(z,3).toString();
                if(jTable1.getValueAt(z,4)==null)holdObject[z][4]="";else holdObject[z][4]=jTable1.getValueAt(z,4).toString();
                if(jTable1.getValueAt(z,5)==null)holdObject[z][5]="";else holdObject[z][5]=jTable1.getValueAt(z,5).toString();
                if(jTable1.getValueAt(z,6)==null)holdObject[z][6]=false;else holdObject[z][6]=jTable1.getValueAt(z,6);
                if(jTable1.getValueAt(z,7)==null)holdObject[z][7]=false;else holdObject[z][7]=jTable1.getValueAt(z,7);
                holdObject[y-1][0]=y;
            }
        }
        tableObject=holdObject;

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "S/N","Attribute", "Type", "Default", "String Length", "Referenced Table", "Optional", "Editable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false,true, true, true, true,true,true,true
            };

            public Class getColumnClass(int columnIndex){
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        jTable1.getColumnModel().getColumn(0).setMinWidth(40);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
        resetType();
        resetRefTable();
        delInUse="N";
    }
    void addRow()
    {
        addInUse="Y";
        System.out.println("Add in use:"+addInUse);
        int x=tableObject.length;
        int y=x+1;
        Object[][] holdObject=new Object[y][8];

        for(int z=0;z<x;z++)
        {
            if(jTable1.getValueAt(z,1)==null||jTable1.getValueAt(z,2)==null)
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"NULL VALUE FOUND ! ","ERROR !",JOptionPane.ERROR_MESSAGE);
                return;
            }
        }

        for(int z=0;z<x;z++)
        {
            if(jTable1.getValueAt(z,1)!=null&&jTable1.getValueAt(z,2)!=null)
            {
                holdObject[z][0]=z+1;
                holdObject[z][1]=jTable1.getValueAt(z,1).toString();
                holdObject[z][2]=jTable1.getValueAt(z,2).toString();
                if(jTable1.getValueAt(z,3)==null)holdObject[z][3]="";else holdObject[z][3]=jTable1.getValueAt(z,3).toString();
                if(jTable1.getValueAt(z,4)==null)holdObject[z][4]="";else holdObject[z][4]=jTable1.getValueAt(z,4).toString();
                if(jTable1.getValueAt(z,5)==null)holdObject[z][5]="";else holdObject[z][5]=jTable1.getValueAt(z,5).toString();
                if(jTable1.getValueAt(z,6)==null)holdObject[z][6]=false;else holdObject[z][6]=jTable1.getValueAt(z,6);
                if(jTable1.getValueAt(z,7)==null)holdObject[z][7]=false;else holdObject[z][7]=jTable1.getValueAt(z,7);
                holdObject[y-1][0]=y;
            }
        }

        //System.out.println(holdObject[0][0]+""+holdObject[0][1]+""+holdObject[0][2]+""+holdObject[0][3]+""+holdObject[0][4]+""+holdObject[0][5]);

        tableObject=holdObject;

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            tableObject,
            new String [] {
                "S/N","Attribute", "Type", "Default", "String Length", "Referenced Table", "Optional", "Editable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false,true, true, true, true,true,true,true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

        jTable1.getColumnModel().getColumn(0).setMinWidth(40);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
        resetType();
        resetRefTable();
        addInUse="N";
    }
    
    void refreshTable()
    {
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", null, null, null, null, null, null, null}
            },
            new String [] {
                "S/N","Attribute", "Type", "Default", "String Length", "Referenced Table", "Optional", "Editable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false,true, true, true, true,true,true,true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableObject=new Object[][]{{"1",null,null,null,null,null,null,null}};
        resetType();
        resetRefTable();
        jTable1.getColumnModel().getColumn(0).setMaxWidth(40);
    }
     void submitData()
    {
        String profileCode=systems.getValue(OpenMSApp.Database_A, Connect.procInit+" prnPrintReferenceTableCode"+Connect.procInitStart+Connect.procInitEnd, "cCode");
        System.out.println("profilecode="+profileCode);
        String tableName=jProfileNameTextField.getText().trim(), prefix=jCodePrefixTextField.getText().trim(), attribute[]=new String[tableObject.length], type[]=new String[tableObject.length], 
                stringLength[]=new String[tableObject.length], defaultValue[]=new String[tableObject.length], refTable[]=new String[tableObject.length], optional[]=new String[tableObject.length], 
                separator[]=new String[tableObject.length],editable[]=new String[tableObject.length];
        String multiRow="0",comment="0";
        String primaryTableCode="";
        int stringLen[]=new int[tableObject.length];
        if(jCheckBox1.isSelected())
        {
            multiRow="1";
            primaryTableCode=systems.getValue(OpenMSApp.Database_A, "Select * from referenceTable where vName='"+jComboBox1.getSelectedItem().toString().trim()+"'", 1);
        }
        if(jCheckBox2.isSelected())
        {
            comment="1";
        }
        int x=tableObject.length;
        int y=0;
        while(y<x)
        {
            try{
            if(jTable1.getValueAt(y,3)==null)defaultValue[y]=" ";
            else if(jTable1.getValueAt(y,2).toString().trim().equalsIgnoreCase("Decimal"))
                defaultValue[y]=systems.converToRealMoneyFormatNeg(jTable1.getValueAt(y,3).toString());
            else 
                defaultValue[y]=jTable1.getValueAt(y,3).toString();
            }
            catch(Exception e){System.out.println("Can not convert money "+e);}
            if(jTable1.getValueAt(y,4)==null)stringLength[y]="NULL";else stringLength[y]=jTable1.getValueAt(y,4).toString();
            if(jTable1.getValueAt(y,4)==null)stringLen[y]=0;
            else if (jTable1.getValueAt(y,4).toString().isEmpty())stringLen[y]=0;
            else stringLen[y]=Integer.parseInt(jTable1.getValueAt(y,4).toString().trim());
            if(jTable1.getValueAt(y,5)==null)refTable[y]=" ";else refTable[y]=jTable1.getValueAt(y,5).toString();
            if(jTable1.getValueAt(y,6)==null)optional[y]="0";else if(jTable1.getValueAt(y,6).equals(true)) optional[y]="1";else  optional[y]="0";
            if(jTable1.getValueAt(y,7)==null)editable[y]="0";else if(jTable1.getValueAt(y,7).equals(true)) editable[y]="1";else  editable[y]="0";

            attribute[y]=jTable1.getValueAt(y,1).toString().trim();
            type[y]=jTable1.getValueAt(y,2).toString().trim();
            separator[y]="ON";                
            //if(jTable1.getValueAt(y,6).equals(true))optional[y]="1";else optional[y]="0";
        y+=1;
        if(y==x){break;}
        }
        //Connect.createMSSQLConnection();
        Connect.changeDB(OpenMSApp.Database_A);
        statement=Connect.createStatement(Connect.procInit+" prnInsReferenceTable"+Connect.procInitStart+"?,?,?,?,?,?"+Connect.procInitEnd);
        try{
            statement.setString(1,jProfileNameTextField.getText().trim());
            statement.setString(2,jCodePrefixTextField.getText().trim());   
            statement.setString(3,"c"+jProfileNameTextField.getText().trim().replace(' ', '_') +"Code"); 
            statement.setString(4,multiRow);
            statement.setString(5,primaryTableCode);
            statement.setString(6,comment);
        }
        catch(Exception e)
        {
            System.out.println("Profile.submitData() gave error ! "+e);
        }
        Connect.executeUpdateStatement(statement);
        y=0;x=tableObject.length;
        while(y<x)
        {                    
        statement=Connect.createStatement(Connect.procInit+" prnInsProfile"+Connect.procInitStart+"'"+profileCode.trim()
                +"','"+jTable1.getValueAt(y,1).toString().trim()+"','"+jTable1.getValueAt(y,2).toString().trim()+"','"
                +defaultValue[y]+"',"+stringLen[y]+",'"+refTable[y]+"','"+optional[y]+"'"+",'"+editable[y]+"',"+(y+1)+Connect.procInitEnd);
        Connect.executeUpdateStatement(statement);
        y+=1;
        if(y==x){break;}
        }
        
        Connect.closeConnection();
        statement=Connect.createStatement("set global sql_mode='ANSI'"); 
        Connect.executeUpdateStatement(statement);
        Connect.closeConnection();
        
        separator[tableObject.length-1]="OFF";
        String queryArray[]=systems.createTableQuery(tableName.trim().replace(' ', '_'), prefix, attribute, type, stringLength, refTable, optional, separator,multiRow).split(";");
        systems.executeSplitedQuery(queryArray, statement);
        
        Connect.alert="Y";
        Connect.closeConnection();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton delButton;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JTextField jCodePrefixTextField;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JPanel jEmployeePanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jProfileNameTextField;
    private javax.swing.JButton jRefreshButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton registerButton;
    // End of variables declaration//GEN-END:variables

}
